home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / database / nfltpw11.zip / NFLLIB.PAS < prev    next >
Pascal/Delphi Source File  |  1992-08-12  |  10KB  |  391 lines

  1. library NflLib;
  2.  
  3. uses strings,WinTypes, WinProcs;
  4.  
  5. {$R c:\tpw\owldemos\BITBTN.RES}
  6. const
  7.   ofState       = 0;
  8.   ofDownBits    = 2;
  9.   ofUpBits      = 4;
  10.   ofFocUpBits   = 6;
  11.   ofSize        = 8; { Amount of window extra bytes to use }
  12.  
  13. const
  14.   bdBorderWidth = 1;
  15.  
  16. const
  17.   bsDisabled    = $0001;
  18.   bsFocus       = $0002;
  19.   bsKeyDown     = $0004;
  20.   bsMouseDown   = $0008;
  21.   bsMouseUpDown = $0010;
  22.   bsDefault     = $0020;
  23.   shSolid       = $0001;
  24.   shFramed      = $0002;
  25.   shFrameIn     = $0004;
  26.  
  27. function ShadeWinFn(HWindow: HWnd; Message: Word; wParam: Word;
  28.   lParam: Longint): Longint; export;
  29.  
  30. var
  31.   DC: HDC;
  32.   Bitmap: TBitmap;
  33.   BitsNumber : integer;
  34.   Rect: TRect;
  35.   Pt: TPoint;
  36.   PS: TPaintStruct;
  37.  
  38.  
  39. function Get(Ofs: Integer): Word;
  40. begin
  41.   Get := GetWindowWord(HWindow, Ofs);
  42. end;
  43.  
  44. function GetWndExtra(Status:word):boolean;
  45. begin
  46.      GetWndExtra := (PCreateStruct(lParam)^.style and Status) = Status;
  47. end;
  48.  
  49. procedure SetWord(Ofs: Integer; Val: Word);
  50. begin
  51.   SetWindowWord(HWindow, Ofs, Val);
  52. end;
  53.  
  54. function State: Word;
  55. begin
  56.   State := Get(ofState);
  57. end;
  58.  
  59. function DownBits: Word;
  60. begin
  61.   DownBits := Get(ofDownBits);
  62. end;
  63.  
  64. function UpBits: Word;
  65. begin
  66.   UpBits := Get(ofUpBits);
  67. end;
  68.  
  69. function FocUpBits: Word;
  70. begin
  71.   FocUpBits := Get(ofFocUpBits);
  72. end;
  73.  
  74. function GetState(AState: Word): Boolean;
  75. begin
  76.   GetState := (State and AState) = AState;
  77. end;
  78.  
  79. procedure Paint(DC: HDC);
  80. var
  81.   Bits : hBitmap;
  82.   BorderBrush,NewBrush, OldBrush: HBrush;
  83.   Frame: TRect;
  84.   Height, Width: Integer;
  85. begin
  86.   Bits := UpBits;
  87.   GetClientRect(HWindow, Frame);
  88.   Height := Frame.bottom - Frame.top;
  89.   Width := Frame.right - Frame.left;
  90.   NewBrush := CreatePatternBrush(Bits);
  91.   OldBrush := SelectObject(DC, NewBrush);
  92.   PatBlt(DC, Frame.left, Frame.Top, Width,height, PatCopy);
  93.   SelectObject(DC, OldBrush);
  94.   DeleteObject(NewBrush);
  95.   if GetState(shFramed) then
  96.   begin
  97.        if GetState(shFrameIn)
  98.        then BorderBrush := GetStockObject(White_Brush)
  99.        else BorderBrush := GetStockObject(Gray_Brush);
  100.        OldBrush := SelectObject(DC,BorderBrush);
  101.        PatBlt(dc, Frame.Left, Frame.Bottom - bdBorderWidth, Width,
  102.                   bdBorderWidth, PatCopy);
  103.        PatBlt(dc, Frame.Right - bdBorderWidth, Frame.top,
  104.                   bdBorderWidth, Height, PatCopy);
  105.        if GetState(shFrameIn)
  106.        then BorderBrush := GetStockObject(Gray_Brush)
  107.        else BorderBrush := GetStockObject(White_Brush);
  108.        SelectObject(dc,BorderBrush);
  109.        PatBlt(dc, Frame.Left, Frame.Top, Width, bdBorderWidth, PatCopy);
  110.        PatBlt(dc, Frame.Left, Frame.Top, bdBorderWidth,Height, PatCopy);
  111.        SelectObject(dc,OldBrush);
  112.   end ;
  113. end;
  114.  
  115. begin
  116.   ShadeWinFn := 0;
  117.   case Message of
  118.     wm_create:
  119.     begin
  120.         if GetWndExtra(shSolid)
  121.         then SetWord(ofUpBits,LoadBitmap(hInstance,pchar(501)))
  122.         else SetWord(ofUpBits,LoadBitmap(hInstance,pchar(500)));
  123.         SetWord(ofState,PCreateStruct(lParam)^.style);
  124.     end;
  125.     wm_Paint:
  126.       begin
  127.         BeginPaint(HWindow, PS);
  128.         Paint(PS.hDC);
  129.         EndPaint(HWindow, PS);
  130.       end;
  131.     wm_EraseBkGnd:
  132.       begin
  133.       end;
  134.     wm_NCDestroy:
  135.       begin
  136.         ShadeWinFn := DefWindowProc(HWindow, Message, wParam, lParam);
  137.         DeleteObject(UpBits);
  138.       end;
  139.   else
  140.     ShadeWinFn := DefWindowProc(HWindow, Message, wParam, lParam);
  141.   end;
  142. end;
  143.  
  144. exports
  145.   ShadeWinFn;
  146.  
  147. function BitButtonWinFn(HWindow: HWnd; Message: Word; wParam: Word;
  148.   lParam: Longint): Longint; export;
  149. var
  150.   DC: HDC;
  151.   Bitmap: TBitmap;
  152.   BitsNumber: Integer;
  153.   Rect: TRect;
  154.   Pt: TPoint;
  155.   PS: TPaintStruct;
  156.  
  157. function Get(Ofs: Integer): Word;
  158. begin
  159.   Get := GetWindowWord(HWindow, Ofs);
  160. end;
  161.  
  162. procedure SetWord(Ofs: Integer; Val: Word);
  163. begin
  164.   SetWindowWord(HWindow, Ofs, Val);
  165. end;
  166.  
  167. function State: Word;
  168. begin
  169.   State := Get(ofState);
  170. end;
  171.  
  172. function DownBits: Word;
  173. begin
  174.   DownBits := Get(ofDownBits);
  175. end;
  176.  
  177. function UpBits: Word;
  178. begin
  179.   UpBits := Get(ofUpBits);
  180. end;
  181.  
  182. function FocUpBits: Word;
  183. begin
  184.   FocUpBits := Get(ofFocUpBits);
  185. end;
  186.  
  187. function GetState(AState: Word): Boolean;
  188. begin
  189.   GetState := (State and AState) = AState;
  190. end;
  191.  
  192. procedure Paint(DC: HDC);
  193. var
  194.   MemDC: HDC;
  195.   Bits, Oldbitmap: HBitmap;
  196.   BorderBrush, OldBrush: HBrush;
  197.   Frame: TRect;
  198.   Height, Width: Integer;
  199. begin
  200.   if (State and (bsMouseDown + bsKeyDown) <> 0) and
  201.       not GetState(bsMouseUpDown) then
  202.     Bits := DownBits
  203.   else
  204.     if GetState(bsFocus) then Bits := FocUpBits
  205.     else Bits := UpBits;
  206.  
  207.   { Draw border }
  208.   GetClientRect(HWindow, Frame);
  209.   Height := Frame.bottom - Frame.top;
  210.   Width := Frame.right - Frame.left;
  211.  
  212.   if GetState(bsDefault) then
  213.     BorderBrush := GetStockObject(Black_Brush)
  214.   else BorderBrush := GetStockObject(White_Brush);
  215.   OldBrush := SelectObject(DC, BorderBrush);
  216.   PatBlt(DC, Frame.left, Frame.top, Width, bdBorderWidth, PatCopy);
  217.   PatBlt(DC, Frame.left, Frame.top, bdBorderWidth, Height, PatCopy);
  218.   PatBlt(DC, Frame.left, Frame.bottom - bdBorderWidth, Width,
  219.     bdBorderWidth, PatCopy);
  220.   PatBlt(DC, Frame.right - bdBorderWidth, Frame.top, bdBorderWidth,
  221.     Height, PatCopy);
  222.   SelectObject(DC, OldBrush);
  223.  
  224.   { Draw bitmap }
  225.   MemDC := CreateCompatibleDC(DC);
  226.   OldBitmap := SelectObject(MemDC, Bits);
  227.   GetObject(Bits, Sizeof(Bitmap), @Bitmap);
  228.   BitBlt(DC, bdBorderWidth, bdBorderWidth, Bitmap.bmWidth, Bitmap.bmHeight,
  229.     MemDC, 0, 0, srcCopy);
  230.   SelectObject(MemDC, OldBitmap);
  231.   DeleteDC(MemDC);
  232. end;
  233.  
  234. procedure Repaint;
  235. var
  236.   DC: HDC;
  237. begin
  238.   DC := GetDC(HWindow);
  239.   Paint(DC);
  240.   ReleaseDC(HWindow, DC);
  241. end;
  242.  
  243. procedure SetState(AState: Word; Enable: Boolean);
  244. var
  245.   OldState: Word;
  246. begin
  247.   OldState := State;
  248.   if Enable then SetWord(ofState, State or AState)
  249.   else SetWord(ofState, State and not AState);
  250.   if State <> OldState then Repaint;
  251. end;
  252.  
  253. function InMe(lPoint: Longint): Boolean;
  254. var
  255.   R: TRect;
  256.   Point: TPoint absolute lPoint;
  257. begin
  258.   GetClientRect(HWindow, R);
  259.   InflateRect(R, -bdBorderWidth, -bdBorderWidth);
  260.   InMe := PtInRect(R, Point);
  261. end;
  262.  
  263. procedure ButtonPressed;
  264. begin
  265.   SetState(bsMouseDown + bsMouseUpDown + bsKeyDown, False);
  266.   SendMessage(GetParent(HWindow), wm_Command, GetDlgCtrlID(HWindow),
  267.     Longint(HWindow));
  268. end;
  269.  
  270. begin
  271.   BitButtonWinFn := 0;
  272.   case Message of
  273.     wm_Create:
  274.       begin
  275.         DC := GetDC(0);
  276.         if (GetSystemMetrics(sm_CYScreen) < 480) or
  277.            (GetDeviceCaps(DC, numColors) < 16) then
  278.           BitsNumber := 2000 + Get(gww_ID)
  279.         else
  280.           BitsNumber := 1000 + Get(gww_ID);
  281.         ReleaseDC(0, DC);
  282.  
  283.         SetWord(ofUpBits, LoadBitmap(hInstance, PChar(BitsNumber)));
  284.         SetWord(ofDownBits, LoadBitmap(hInstance, pChar(BitsNumber + 2000)));
  285.         SetWord(ofFocUpBits, LoadBitmap(hInstance, pChar(BitsNumber + 4000)));
  286.         GetObject(DownBits, SizeOf(Bitmap), @Bitmap);
  287.         GetWindowRect(HWindow, Rect);
  288.         Pt.X := Rect.Left;
  289.         Pt.Y := Rect.Top;
  290.         ScreenToClient(PCreateStruct (lParam)^.hwndParent, Pt);
  291.         MoveWindow(HWindow, Pt.X, Pt.Y,
  292.           Bitmap.bmWidth + bdBorderWidth * 2,
  293.           Bitmap.bmHeight + bdBorderWidth * 2, False);
  294.         if (PCreateStruct(lParam)^.style and $1F) = bs_DefPushButton then
  295.           SetState(bsDefault, True);
  296.       end;
  297.     wm_NCDestroy:
  298.       begin
  299.         BitButtonWinFn := DefWindowProc(HWindow, Message, wParam, lParam);
  300.         DeleteObject(UpBits);
  301.         DeleteObject(DownBits);
  302.         DeleteObject(FocUpBits);
  303.       end;
  304.     wm_Paint:
  305.       begin
  306.         BeginPaint(HWindow, PS);
  307.         Paint(PS.hDC);
  308.         EndPaint(HWindow, PS);
  309.       end;
  310.     wm_EraseBkGnd:
  311.       begin
  312.       end;
  313.     wm_Enable:
  314.       SetState(bsDisabled, wParam <> 0);
  315.     wm_SetFocus:
  316.       SetState(bsFocus, True);
  317.     wm_KillFocus:
  318.       SetState(bsFocus, False);
  319.     wm_KeyDown:
  320.       if (wParam = $20) and not GetState(bsKeyDown) and
  321.           not GetState(bsMouseDown) then
  322.         SetState(bsKeyDown, True);
  323.     wm_KeyUP:
  324.       if (wParam = $20) and GetState(bsKeyDown) then
  325.         ButtonPressed;
  326.     wm_LButtonDblClk, wm_LButtonDown:
  327.       if InMe(lParam) and not GetState(bsKeyDown) then
  328.       begin
  329.         if GetFocus <> HWindow then SetFocus(HWindow);
  330.         SetState(bsMouseDown, True);
  331.         SetCapture(HWindow);
  332.       end;
  333.     wm_MouseMove:
  334.       if GetState(bsMouseDown) then
  335.         SetState(bsMouseUpDown, not InMe(lParam));
  336.     wm_LButtonUp:
  337.       if GetState(bsMouseDown) then
  338.       begin
  339.         ReleaseCapture;
  340.         if not GetState(bsMouseUpDown) then ButtonPressed
  341.         else SetState(bsMouseDown + bsMouseUpDown, False);
  342.       end;
  343.     wm_GetDlgCode:
  344.       if GetState(bsDefault) then
  345.         BitButtonWinFn:= dlgc_DefPushButton
  346.       else
  347.         BitButtonWinFn := dlgc_UndefPushButton;
  348.     bm_SetStyle:
  349.       SetState(bsDefault, wParam = bs_DefPushButton);
  350.   else
  351.     BitButtonWinFn := DefWindowProc(HWindow, Message, wParam, lParam);
  352.   end;
  353. end;
  354.  
  355. exports
  356.   BitButtonWinFn;
  357.  
  358. var
  359.   Class: TWndClass;
  360.  
  361. begin
  362.   with Class do
  363.   begin
  364.     lpszClassName := 'BitButton';
  365.     hCursor       := LoadCursor(0, idc_Arrow);
  366.     lpszMenuName  := nil;
  367.     style         := cs_HRedraw or cs_VRedraw or cs_DblClks or cs_GlobalClass;
  368.     lpfnWndProc   := TFarProc(@BitButtonWinFn);
  369.     hInstance     := System.hInstance;
  370.     hIcon         := 0;
  371.     cbWndExtra    := ofSize;
  372.     cbClsExtra    := 0;
  373.     hbrBackground := 0;
  374.   end;
  375.   RegisterClass(Class);
  376.   with Class do
  377.   begin
  378.     lpszClassName := 'MyShade';
  379.     hCursor       := LoadCursor(0, idc_Arrow);
  380.     lpszMenuName  := nil;
  381.     style         := cs_HRedraw or cs_VRedraw or cs_GlobalClass;
  382.     lpfnWndProc   := TFarProc(@ShadeWinFn);
  383.     hInstance     := System.hInstance;
  384.     hIcon         := 0;
  385.     cbWndExtra    := ofSize;
  386.     cbClsExtra    := 0;
  387.     hbrBackground := 0;
  388.   end;
  389.   RegisterClass(Class);
  390. end.
  391.